/*
 * @Author: Lin Hongwei
 * @Date: 2021-12-06 15:11:40
 * @LastEditTime: 2021-12-09 10:27:45
 * @LastEditors: Lin Hongwei
 * @email: 1365755396@qq.com
 * @CSDN: https://blog.csdn.net/weixin_45429409
 * @Description: webpack配置公共文件
 */
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");//打包html
const { VueLoaderPlugin } = require("vue-loader");//打包vue文件插件
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
// const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");

function resolve(dir){
    return  path.resolve(__dirname, dir);
}

const generateLoaders = ()=>{
    return [
        {
            loader: MiniCssExtractPlugin.loader,
            options: {
                publicPath: "../"
            }
        },
        "css-loader",
        "postcss-loader"
    ]
}

module.exports = {
    entry: resolve("src/main.js"),
    output: {
        filename: "js/index.js",
        path: resolve("dist")
    },
    module: {
        rules: [
            {
                //es6解析
                test: /\.m?js$/,
                exclude: /node_modules/,
                use: {
                    loader: "babel-loader",
                    options: {
                        presets: ['@babel/preset-env']
                    }
                }
            },
            {
                test: /\.vue$/,
                use: ["vue-loader"]
            },
            {
                test: /\.(jpg|png|jpeg|gif|avg)$/,
                loader: "url-loader",
                options: {
                    name: "[name][hash:5].[ext]",
                    limit: 1024,
                    outputPath: "images",
                    esModule: false
                },
                type: "javascript/auto"
            },
            {
                test: /\.css$/,
                use: generateLoaders()
            },
            {
                test: /\.s(a|c)ss$/,
                use: [
                    ...(generateLoaders()),
                    "sass-loader",
                    {
                        loader:"sass-resources-loader",
                        options:{
                            resources:[resolve("src/assets/style/global.scss")]
                        }
                    }
                ]
            },
            {
                test: /\.(woff2|woff|ttf)$/,
                loader: "url-loader",
                options: {
                    name: "[name][hash:5].[ext]",
                    limit: 1024,
                    outputPath: "icon",
                    esModule: false,
                },
                type: "javascript/auto"
            },
            // {
            //     test: /\.epub*$/,
            //     include: resolve("static"),
            //     exclude:/node_modules/,
            //     loader: "file-loader",
            //     options: {
            //         name: "[name].[ext]",
            //         outputPath: "static/epub",
            //         esModule: false,
            //         // publicPath: "../"
            //     },
            //     type: "javascript/auto"
            // }
        ]
    },
    plugins: [
        new VueLoaderPlugin(),
        new HtmlWebpackPlugin({
            title: "epub电子书阅读器",
            template: resolve( "public/index.html"),
            minify: {
                collapseWhitespace: false,
                removeComments: false,
                removeAttributeQuotes: false
            },
            hash: true
        }),
        new MiniCssExtractPlugin({
            filename: "./css/[name].css"
        }),
        // new CleanWebpackPlugin(),
        new CopyWebpackPlugin({
            patterns: [
              { 
                  from: path.resolve(__dirname,"static"), 
                  to:  path.resolve(__dirname,"dist/static"),
              },
            ],
        }),
    ],
    resolve: {
        alias: {
            "@": resolve("src"),
            "@static": resolve("static")
        }
    }
}
